package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.dao.LiveDao;
import com.example.entity.*;
import com.example.service.ReserveliveService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 日期：2025/5/11 17:55
 */

@CrossOrigin
@RestController
@RequestMapping("/reservelive")
public class ReserveLiveController {

    @Resource
    private ReserveliveService reserveliveService;

    @Resource
    private LiveDao liveDao;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Reservelive> info = reserveliveService.findBySearch(params);
        return Result.success(info);
    }

    // 新增或修改
    @PostMapping
    public Result save(@RequestBody Reservelive reservelive){
        Live live = liveDao.selectByPrimaryKey(reservelive.getLiveId());

        if (live.getNum() == 0){
            return Result.error("数量不能为0");
        }

        reservelive.setTime(DateUtil.now());
        reserveliveService.add(reservelive);

        live.setNum(live.getNum() - 1);
        liveDao.updateByPrimaryKey(live);

        return Result.success();
    }



    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        reserveliveService.delete(id);
        return Result.success();
    }
}
